package com.cmol.peopledetectapp.user;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cmol.data.GlobalData;
import com.cmol.data.SettingData;
import com.cmol.net.Network;
import com.cmol.peopledetectapp.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 用户历史记录
 *
 * @author 王国良
 */
public class HistoryActivity extends AppCompatActivity {
    private TextView lastTime, queryCount, queryFrePlace, registerId;
    private TextView title, lastTimeText, queryCountText, queryFrePlaceText;
    private Network network;
    private View view;
    private String lT = new String(), qC = new String(), qFP = new String();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        lastTime = findViewById(R.id.queryTime);
        queryCount = findViewById(R.id.queryFre);
        queryFrePlace = findViewById(R.id.queryFrePlace);
        registerId = findViewById(R.id.registerId);
        view = findViewById(R.id.HistoryBackground);
        title = findViewById(R.id.titleText);
        lastTimeText = findViewById(R.id.lastTimeText);
        queryCountText = findViewById(R.id.historyQueryText);
        queryFrePlaceText = findViewById(R.id.mostFText);

        init();
        SystemSetting();
    }

    public void init() {
        JSONObject tmp = new JSONObject();
        network = new Network(tmp, "/user/historyLog") {
            @Override
            protected void process(Call call, Response response) throws IOException {
                String ans = Objects.requireNonNull(response.body()).string().trim();
                /**
                 * 返回报文格式
                 * historyLog :
                 * lastTime :
                 * queryCount :
                 * queryFrePlace :
                 */
                try {
                    JSONObject js = new JSONObject(ans);
                    lT = js.getString("lastTime");
                    qC = js.getString("queryCount");
                    qFP = js.getString("queryFrePlace");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void failureWarn() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(HistoryActivity.this, "网络请求异常", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lastTime.setText(lT);
        queryCount.setText(qC);
        queryFrePlace.setText(qFP);
        // 根据中英文设置显示
        if (SettingData.getLANGUAGE() == true) {
            registerId.setText("您是第" + GlobalData.getUserToken() + "个注册的用户");
        } else {
            registerId.setText("You're the NO." + GlobalData.getUserToken() + "User");
        }

    }

    private void SystemSetting() {
        if (SettingData.getCOLOR() == false) {
            view.setBackgroundResource(R.drawable.redbackground);
        } else {
            view.setBackgroundResource(R.drawable.background_iner);
        }

        if (SettingData.getLANGUAGE() == false) {
            title.setText("History");
            lastTimeText.setText("Last Query Time");
            queryCountText.setText("History Query Time");
            queryFrePlaceText.setText("Most Query Place");
        } else {
            title.setText("历史记录");
            lastTimeText.setText("上次查询时间");
            queryCountText.setText("历史查询次数");
            queryFrePlaceText.setText("最高频查询地点");
        }
    }
}